//
//  UPApplyBaseViewController.m
//  Uang Pasti
//
//  Created by 李永彬 on 2025/8/25.
//

#import "UPApplyBaseViewController.h"
#import "UPButtonCollectionViewCell.h"
#import "UPGoToApplyViewController.h"

@interface UPApplyBaseViewController () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@end

@implementation UPApplyBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.bgImageView.image = [UIImage imageNamed:@"apply_bg"];
    self.title = self.titleName;
    
    [self up_setupCollectionView];
    [self setupCustomBackButton];
}

- (void)up_setupCollectionView {
    // 创建流式布局
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(0, 15, 0, 15);
    
    // 创建CollectionView
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor clearColor];
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.collectionView registerClass:[UPButtonCollectionViewCell class] forCellWithReuseIdentifier:@"UPButtonCell"];
    [self.view addSubview:self.collectionView];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(20);
        make.left.right.equalTo(self.view);
        make.height.equalTo(@102);
    }];
}

- (void)setupCustomBackButton {
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_back"]
                                                                   style:UIBarButtonItemStylePlain
                                                                  target:self
                                                                  action:@selector(backButtonTapped)];
    self.navigationItem.leftBarButtonItem = backButton;
    self.navigationItem.hidesBackButton = YES;
}

- (void)backButtonTapped {
    [self.view endEditing:YES];
    [self showDetainmentAlertView];
}

- (void)showDetainmentAlertView {
    UPCustomAlertView *alert = [[UPCustomAlertView alloc] initWithBackgroundImage:[UIImage imageNamed:@"alert_bg_image1"] style:UPAlertViewStyleCenter];
    [alert setTitle:LocalizableForKey(@"auth_confirm_title") font:[UIFont boldSystemFontOfSize:13]];
    [alert setContent:LocalizableForKey(@"auth_confirm_content") font:[UIFont boldSystemFontOfSize:13]];
    [alert setContent:LocalizableForKey(@"NSCameraUsageDescription") font:[UIFont systemFontOfSize:13]];
    
    __block typeof(self) weakSelf = self;
    [alert addButtonWithTitle:LocalizableForKey(@"Continue Certifcation")
                        style:UPButtonStyleDefault
                      handler:^{
    }];

    [alert addButtonWithTitle:LocalizableForKey(@"Give Up Certifcation")
                        style:UPButtonStyleDestructive
                      handler:^{
        [weakSelf.navigationController popToViewControllerOfClass:[UPGoToApplyViewController class] animated:true];
    }];

    // 显示弹窗
    [alert showInView:self.view];
}


#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.currentLoanDetailModel.stultiive.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UPButtonCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UPButtonCell" forIndexPath:indexPath];
    StultiiveModel *model = self.currentLoanDetailModel.stultiive[indexPath.item];
//    BOOL isSelected = (indexPath.item == self.selectedIndex);
//    [cell up_configureWithTitle:title isSelected:isSelected];
    NSString *imageName = [model getImageNormal];
    if ([model.saccular isEqualToString:self.vcName]) {
        imageName = [model getImageSelected];
    }
    [cell up_configureWithImageName:imageName];
    
    
    return cell;
}

#pragma mark - UICollectionViewDelegateFlowLayout

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(94, 102);
}

#pragma mark - UICollectionViewDelegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
//    [collectionView scrollToItemAtIndexPath:indexPath
//                           atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally
//                                   animated:YES];
}

@end
